package com.xuxin.baby.photo;

import android.Manifest;
import android.app.Activity;
import android.content.Context;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.maning.imagebrowserlibrary.MNImageBrowser;
import com.maning.imagebrowserlibrary.model.ImageBrowserConfig;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.utils.PathUtils;

import java.util.List;

/**
 * Created by xu_xin
 * Created on 2020/10/29 0029
 * 拍照图库
 */
public class PhotoUtils {

    public interface OnResultListener {
        void onResult(List<LocalMedia> result);
    }

    public static void openPhoto(Context context, OnResultListener onResultListener) {
        PermissionUtils.permission(Manifest.permission.CAMERA).callback(new PermissionUtils.SimpleCallback() {
            @Override
            public void onGranted() {
                new XPopup.Builder(context).asCenterList("上传图片", new String[]{"拍照", "图库"},
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                if (position == 0) {
                                    PhotoUtils.openCamera(context, onResultListener);
                                } else {
                                    PhotoUtils.openGallery(context, onResultListener);
                                }
                            }
                        })
                        .show();
            }

            @Override
            public void onDenied() {
                ToastUtils.showShort("权限已被拒绝，请打开权限");
            }
        }).request();
    }

    /**
     * 拍照
     */
    public static void openCamera(Context context, OnResultListener onResultListener) {
        PictureSelector.create((Activity) context)
                .openCamera(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .selectionMode(PictureConfig.SINGLE)
                .isCompress(true)
                .minimumCompressSize(100)
                .renameCompressFile(PathUtils.getImageName())
                .compressSavePath(PathUtils.getPath())
                .imageEngine(GlideEngine.createGlideEngine())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> result) {
                        if (null != onResultListener) {
                            onResultListener.onResult(result);
                        }
                    }

                    @Override
                    public void onCancel() {
                        LogUtils.e("取消上传");
                    }
                });
    }

    /**
     * 图库
     */
    public static void openGallery(Context context, OnResultListener onResultListener) {
        PictureSelector.create((Activity) context)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .selectionMode(PictureConfig.SINGLE)
                .isCompress(true)
                .minimumCompressSize(100)
                .renameCompressFile(PathUtils.getImageName())
                .compressSavePath(PathUtils.getPath())
                .imageEngine(GlideEngine.createGlideEngine())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(List<LocalMedia> result) {
                        if (null != onResultListener) {
                            onResultListener.onResult(result);
                        }
                    }

                    @Override
                    public void onCancel() {
                        LogUtils.e("取消上传");
                    }
                });
    }

    public static void browser(Context context, String image) {
        if (!StringUtils.isEmpty(image)) {
            MNImageBrowser.with(context)
                    .setCurrentPosition(0)
                    .setImageEngine(new BrowserEngine())
                    .setImageUrl(Retrofit.image_url + image)
                    .setTransformType(ImageBrowserConfig.TransformType.Transform_ZoomIn)
                    .setIndicatorType(ImageBrowserConfig.IndicatorType.Indicator_Circle)
                    .setIndicatorHide(false)
                    .setScreenOrientationType(ImageBrowserConfig.ScreenOrientationType.ScreenOrientation_Portrait)
                    .setWindowBackgroundColor("#000000")
                    .show();
        }
    }
}